<?php

$title = 'Dystrybucja AP';
require_once("includes/head.php"); if (!$_GET['ajax']) require_once("includes/head_start.php"); 

require_once("languages/".$player -> lang."/ap.php");

if (!$player -> race || !$player -> clas) {
    error (NO_CLASS);
}

if(!isset($_GET['step']))
{
//show current stats for ap
$ap = $db -> GetRow("SELECT `agility`, `speed`, `endurance`, `strength`, `int`, `wisdom` FROM `zywioly` WHERE `name`='".$player -> race."'") or die($db -> ErrorMsg());

//modyfikatory klas
if(!isset ($_GET['step']) && $player -> clas == 'Czarodziej' && $player -> ap > 0)
{
  $ap['int'] += 0.5; 
}

$smarty -> assign(array("Strength" => $ap['strength'],
 "Agility" => $ap['agility'],
 "Speed" => $ap['speed'],
 "Endurance" => $ap['endurance'],
 "Stat2" => $ap['wisdom'],
 "Stat" => $ap['int']));

}

 /**
* Distribution of Astral Points
*/
if (isset ($_GET['step']) && $_GET['step'] == 'add') 
{
    if (!isset($_POST['strength']) || !isset($_POST['agility']) || !isset($_POST['szyb']) || !isset($_POST['wytrz']) || !isset($_POST['inteli']) || !isset($_POST['wisdom'])) 
    {
        error(EMPTY_FIELDS);
    }
    $arrchar = array($_POST['strength'],$_POST['agility'],$_POST['szyb'],$_POST['wytrz'],$_POST['inteli'],$_POST['wisdom']);
    $arrgain = array(0,0,0,0,0,0);
    $sum = 0;
    foreach ($arrchar as $stat) 
    {
        if (!ereg("^[0-9]*$", $stat)) 
        {
            error (ERROR);
        }
        $sum = ($sum + $stat);
    }
    if ($sum > $player -> ap) 
    {
        error (NO_AP);
    }
    if ($sum == 0) 
    {
        error (NO_AP2);
    }
	
	$ap = $db -> GetRow("SELECT `agility`, `speed`, `endurance`, `strength`, `int`, `wisdom` FROM `zywioly` WHERE `name`='".$player -> race."'") or die($db -> ErrorMsg());
	
	    $arrgain[0] = $ap['strength']; //sila
	    $arrgain[1] = $ap['agility']; //zrecznosc
	    $arrgain[2] = $ap['speed']; //szybkosc
	    $arrgain[3] = $ap['endurance']; //wytrzymalosc
		$arrgain[4] = $ap['int']; //inteligencja
		$arrgain[5] = $ap['wisdom']; //sila woli 
	
//Modyfikatory klas
/*A to co jest? :P Anor
	if ($player -> clas == 'Czarodziej' && $player -> race !='Demon' && $player -> race !='Duergar' && $player -> race !='Hobbit' && $player -> race !='Krasnolud' ) 
	{
		$arrgain[4] = $arrgain[4] + 0.5;
	}
*/
  
  
//reszta
   $arrpoints = array(0,0,0,0,0,0);
    $arrname = array(A_STRENGTH, A_AGILITY, A_SPEED, A_CONDITION, A_INTELIGENCE, A_WISDOM);
    for ($i=0;$i<6;$i++) 
    {
        $arrpoints[$i] = $arrchar[$i] * $arrgain[$i];
    }
	
	
	    if ($arrpoints[0] > 0) 
    {
	    $db -> Execute("UPDATE players SET strength=strength+".$arrpoints[0]." WHERE id=".$player -> id);
    }
    if ($arrpoints[1] > 0) 
    {
	    $db -> Execute("UPDATE players SET agility=agility+".$arrpoints[1]." WHERE id=".$player -> id);
    }
    if ($arrpoints[2] > 0) 
    {
	    $db -> Execute("UPDATE players SET szyb=szyb+".$arrpoints[2]." WHERE id=".$player -> id);
    }
    if ($arrpoints[3] > 0) 
    {
        $db -> Execute("UPDATE players SET max_hp=max_hp+".$arrpoints[3].", wytrz=wytrz+".$arrpoints[3]." WHERE id=".$player -> id);
    }
    if ($arrpoints[4] > 0) 
    {
	    $db -> Execute("UPDATE players SET inteli=inteli+".$arrpoints[4]." WHERE id=".$player -> id);
    }
    if ($arrpoints[5] > 0) 
    {
	    $db -> Execute("UPDATE players SET wisdom=wisdom+".$arrpoints[5]." WHERE id=".$player -> id);
    }
	
	    $db -> Execute("UPDATE players SET ap=ap-".$sum." WHERE id=".$player -> id);
    $smarty -> assign (array("Amount" => $arrpoints, 
        "Name" => $arrname,
        "Youget" => YOU_GET,
        "Click" => CLICK,
        "Here" => HERE,
        "Fora" => FOR_A));
		
}




/**
* Initialization of variable and assign variables to template
*/
if (!isset($_GET['step'])) 
{
    $_GET['step'] = '';
	$smarty -> assign(array("Apinfo" => AP_INFO,
        "Ap2" => AP,
        "Nstrength" => N_STRENGTH,
        "Nagility" => N_AGILITY,
        "Nspeed" => N_SPEED,
        "Ncond" => N_COND,
        "Nint" => N_INT,
        "Nwisdom" => N_WISDOM,
        "Aadd" => A_ADD));
}

/**
* Assign variables and display page
*/
$smarty -> assign (array("Step" => $_GET['step'], 
    "Ap" => $player -> ap));
$smarty -> display ('ap.tpl');

if (!$_GET['ajax']) require_once("includes/foot.php");
